/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package tableModels;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.table.AbstractTableModel;
import utils.Connexion;
import utils.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Betty
 */
public class TableLieuAVisiterModel extends AbstractTableModel {
    private Connexion _connexion;
    private  Object[][] data;
    private  Object[] colname;

    public TableLieuAVisiterModel(Connexion connect, String query,String[] col) throws SQLException
    {
        this.colname = col;
        this._connexion = connect;
        this._connexion.setQuery(query);
    }

    public int getColumnCount()
    {
        return this.colname.length;
    }

    public int getRowCount()
    {
        int i = 0;
        try {
            ResultSet rs = this._connexion.getResultSet();
            rs.last();
            i = rs.getRow();
        }catch(SQLException e){
        }
        return i;
    }

    @Override
    public String getColumnName(int c){
        return (String) this.colname[c];
    }

    public Object getValueAt(int row,int column)
    {
        Object o = "";
        if (this.getColumnName(column) == "Départ" || this.getColumnName(column) == "Arrivee") {
            try {
                ResultSet rs = this._connexion.getResultSet();
                rs.absolute(row + 1);
                o = rs.getObject(column + 1) + " - " + rs.getObject(column + 2);
            }catch(SQLException e){
                System.out.println(e);
            }
        } else {
             try {
                ResultSet rs = this._connexion.getResultSet();
                rs.absolute(row + 1);
                o = rs.getObject(column + 1) + " - " + rs.getObject(column + 2);
            }catch(SQLException e){
                System.out.println(e);
            }
        }
        try {
            ResultSet rs = this._connexion.getResultSet();
            rs.absolute(row + 1);
            o = rs.getObject(column + 1);
        }catch(SQLException e){
            System.out.println(e);
        }
        return o;
    }
}
